package net.zhengxinyun.performance.controller.yearSummaryScore;

import net.zhengxinyun.performance.Util.DateUtils;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.dto.HeadColums;
import net.zhengxinyun.performance.entity.*;
import net.zhengxinyun.performance.filter.*;
import net.zhengxinyun.performance.service.departClass.DepartHeadService;
import net.zhengxinyun.performance.service.department.DepartmentService;
import net.zhengxinyun.performance.service.indicators.TownScoreService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.yearSummaryScore.YearSummaryScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 总分表控制层
 */
@RestController
@RequestMapping("/departScore")
public class YearSummaryScoreController {
    private TokenService tokenService;
    private LoginService loginService;
    private YearSummaryScoreService yearSummaryScoreService;
    private TownScoreService townScoreService;
    private DepartmentService departmentService;

    @Autowired
    private DepartHeadService headService;

    @Autowired
    public YearSummaryScoreController(DepartmentService departmentService,TownScoreService townScoreService,TokenService tokenService, LoginService loginService, YearSummaryScoreService yearSummaryScoreService) {
        this.tokenService = tokenService;
        this.loginService = loginService;
        this.yearSummaryScoreService = yearSummaryScoreService;
        this.townScoreService=townScoreService;
        this.departmentService=departmentService;
    }

    @PostMapping("/query")
    public Result<HashMap<String, Object>> query(@RequestBody YearSummaryScoreParam param, HttpServletRequest request) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String year = (String) request.getSession().getAttribute("currentYear");
            if(year==null)
            {
                return ResultUtils.getResults(Result.Status.OVERTIME,"登录超时，请重新登录",null);
            }
            String datatime =param.filter.getDatetime();
            if(StringUtils.isBlank(param.filter.getDatetime())){
                 datatime = DateUtils.getLastMonth();
            }

            List<DepartClassHead> heads = headService.queryByDatetime(year);
            HashMap<String, Object> departScore = yearSummaryScoreService.getDepartScore(heads, datatime,param.filter.getDepartcode(),null,null);
           // map.put("data",departScore);

            return ResultUtils.getSuccessResults(departScore);

        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);

    }


    @PostMapping("/querydemo")
    public Result<HashMap<String, Object>> querydemo(@RequestBody YearSummaryScoreParam param) {
//        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            HashMap<String,Object> map = new HashMap<>();
            List<HeadColums>  tableHead = new ArrayList<>();
            List<Map<String,Object>> dataMap = new ArrayList<>();
            for (int i=0;i<5;i++){
                Map<String,Object> itemMap = new LinkedHashMap<>();
                itemMap.put("id",i+1);
                itemMap.put("name","沙雕杰"+(i+1)+"号");
                itemMap.put("age",65+i);
                itemMap.put("addr","沙雕街"+3+i+"号");

                dataMap.add(itemMap);
            }

            Map<String, Object> map1 = dataMap.get(0);
            for(Map.Entry<String,Object> entry:map1.entrySet())
            {
                tableHead.add(new HeadColums(entry.getKey(),entry.getKey()));


            }
            map.put("tableHead",tableHead);
            map.put("tableData",dataMap);
        // 指标得分
        DepartmentEntity name = departmentService.getDepartByName("财政局");

        //正向激励
       // String indexScore = yearSummaryScoreService.getIncetiveScore("201907", name);

        yearSummaryScoreService.getUintScore("201907",name);
        //县直单位

      //  map.put("score",indexScore);


        return ResultUtils.getSuccessResults(map);

//        }
//        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);

    }

 /*
    public void demoTable(){

        List<String> heads = new ArrayList<>();
        List<Datademo> datas = new ArrayList<>();
        Datademo d1 = new Datademo("shadiaojie ","程伟节");
        Datademo d2 = new Datademo("沙雕杰","死傻逼");


    }*/


}

/*class Datademo{
    private String id;
    private String name;

    public Datademo() {
    }

    public Datademo(String id, String name) {
        this.id = id;
        this.name = name;
    }
}*/


class YearSummaryScoreParam {
    public TokenEntity tokenEntity;
    public DepartmentFilter filter;

}
